<?php

/**
 * 魔法召唤 价格配置
 */

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\facade\Db;

class MagicconController extends AdminbaseController
{
	// 分类
	protected function types()
	{
		return ['common' => '普通', 'high' => '高级', 'luxury' => '豪华'];
	}
	
	// 配置列表
	function index()
	{
		$lists = Db::name("magic_con")->order("list_order asc,id asc")->paginate(20);
		$page  = $lists->render();
		
		$this->assign('lists', $lists);
		$this->assign('types', $this->types());
		$this->assign("page", $page);
		
		return $this->fetch();
	}
	
	//排序
	public function listOrder()
	{
		
		$model = DB::name('magic_con');
		parent::listOrders($model);
		
		$action = "更新魔法召唤价格列表排序 ";
		setAdminLog($action);
		$this->resetcache();
		$this->success("排序更新成功！");
	}
	
	// 修改展示
	function edit()
	{
		$id   = $this->request->param('id', 0, 'intval');
		$data = Db::name('magic_con')->where("id={$id}")->find();
		if(!$data){
			$this->error("信息错误");
		}
		
		$this->assign('data', $data);
		
		return $this->fetch();
	}
	
	// 执行修改
	function editPost()
	{
		if($this->request->isPost()){
			
			$data  = $this->request->param();
			/*$times = $data['times'];
			if($times < 1){
				$this->error("请输入正确的次数");
			}*/
			$coin = $data['coin'];
			$coupon = $data['coupon'];
			if($coin < 1){
				$this->error("请输入正确的价格");
			}
			if($coupon < 1){
				$this->error("请输入正确的价格");
			}
			
			$rs = DB::name('magic_con')->update($data);
			if($rs === false){
				$this->error("修改失败！");
			}
			
			$action = "修改魔法召唤价格列表排序ID: ".$data['id'];
			setAdminLog($action);
			
			$this->resetcache();
			$this->success("修改成功！");
		}
	}
	
	// 重新缓存
	function resetcache()
	{
		$key  = 'magic_con';
		$list = DB::name('magic_con')->field("id,times,coin,coupon,itype")->order('list_order asc,id asc')->select();
		if($list){
			setcaches($key, $list);
		} else{
			delcache($key);
		}
		
		return 1;
	}
}
